package com.tms.app.tms.ui.home.msg;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.tms.app.tms.R;
import com.tms.app.tms.adapter.OrderChildAdapter;
import com.tms.app.tms.bean.Msg;
import com.tms.app.tms.bean.NotionBean;
import com.tms.app.tms.db.MsgDBUtil;
import com.tms.app.tms.ui.base.ui.BaseListFragmnet;
import com.tms.app.tms.ui.base.ui.TabFragment;
import com.tms.app.tms.ui.home.MainActivity;
import com.tms.app.tms.utils.SharePreferenceUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by happy on 2017/8/26.
 */

public class OrderChildFt extends TabFragment {
    Unbinder unbinder;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    private OrderChildP orderP;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        orderP = new OrderChildP();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.layout_fragment_order_child, null);
        unbinder = ButterKnife.bind(this, rootView);
        initView();
        orderP.setiView(this);

        return rootView;
    }

    @Override
    public void onStart() {
        super.onStart();
       // hideLoadingDialog();
        initData();
        orderP.getData(SharePreferenceUtil.getToken(getContext()),1);
       // showLoadingDialog();
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void  showNotion(NotionBean notionBean){
        orderP.onRefresh();
        if (!isHidden()){
            //showLoadingDialog();
        }

    }

    private void initView() {
        LinearLayoutManager mgr = new LinearLayoutManager(getActivity());
        adapter = new OrderChildAdapter();
        adapter.setOnItemChildClickListener(orderP);
        adapter.setOnItemClickListener(orderP);
        recyclerView.setLayoutManager(mgr);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setAdapter(adapter);
        swipeLayout.setOnRefreshListener(orderP);
    }

    private void initData(){
        showLoadingDialog();
        List<Msg> msgList= MsgDBUtil.getMsgListByType(0);
        if (msgList!=null&&msgList.size()>0){
            adapter.getData().clear();
            adapter.getData().addAll(msgList);
            adapter.notifyDataSetChanged();
            hideNerError();
        }else {
            showNetError(0,"无数据");
        }
        hideLoadingDialog();
    }




    @Override
    public void handlerErrorMsg(int errorCode) {

    }


    @Override
    public void refreshComplite(String... msg) {
        super.refreshComplite(msg);
        hideNerError();
        swipeLayout.setRefreshing(false);
    }

    @Override
    public void refreshNoData(String... msg) {
        super.refreshNoData(msg);
        swipeLayout.setRefreshing(false);
    }
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden){

        }
    }
    @Override
    protected void lazyLoad() {
//        MainActivity activity= (MainActivity) getActivity();
//        activity.hideMsgNum();
    }
}
